﻿Chenxin.UI.lockedManage = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: 'service/LockedHandler.ashx',
    createGrid: function() {
        //        return new Chenxin.UI.Pagination({
        //            actionUrl: this.actionUrl + '?op=getlist',
        //            domId: 'page',
        //            data: {},
        //            renderTo: 'lock_table',
        //            render: function(data) {
        //                document.getElementById('lock_table').innerHTML = data;
        //            }
        //        });
        return new Chenxin.UI.ExpandGrid({
            actionUrl: this.actionUrl,
            pageSize: 50,
            height: 400,
            id: 'lockList',
            baseParam: { op: 'getlist' },
            renderTo: 'lock_table',
            hasCheckBox: false,
            scope: this,
            cellBorder: true,
            columns: {
                icon: { width: 50, head: '操作'},
                HouseCode: { width: 200, head: '房源编号 ', sortable: true, sortField: 'A.HouseCode' },
                HouseAddress: { width: 200, head: '房源地址', sortable: true, sortField: 'A.HouseAddress' },
                LockedStatus: { width: 50, head: '状态 ', sortable: true, sortField: 'A.LockedStatus' },
                LockedTime: { width: 90, head: '封盘时间', sortable: true, sortField: 'A.LockedTime' },
                Asker: { width: 70, head: '请求人 ', sortable: true, sortField: 'A.Asker' },
                AskerDepart: { width: 90, head: '所属部门 ', sortable: true, sortField: 'A.AskerDepart' },
                Operator: { width: 70, head: '操作者 ', sortable: true, sortField: 'SalePrice_T,RentPrice' },
                LockedTime_1: { width: 90, head: '封盘开始', sortable: true, sortField: 'A.LockedTime' }, 
                LastTime: { width: 90, head: '封盘结束', sortable: true, sortField: 'A.LastTime' }
            }
        });
    },
    createForm: function() {
        return new Chenxin.UI.HouseBaseInfoForm({
            scope: this,
            autoInit: true,
            saveSucc: function() {
                this.reloadGrid();
            }
        });
    },
    createSearch: function() {
        return new Chenxin.UI.lockedSearch({
            scope: this
        });
    },
    //    loadkeyinfo: function() {
    //        var _this = this;
    //        _this.renderHouseInfo();
    //        _this.createKeyUseList(this.currentId);
    //    },
    loadHouseInfo: function() {
        var _this = this;
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: 'service/housemgr.ashx',
            data: { op: 'gethouseinfo', houseid: this.currentId },
            onSuccess: function(data, msg) {
                if (data.houseinfo) {
                    _this.currentHouseInfo = data.houseinfo;
                    _this.renderHouseInfo();
                }
                _this.createTraceList(data.countTrace);
            }
        });
    },
    renderHouseInfo: function() {
        var m = this.currentHouseInfo;
        var infoStr = '';
        infoStr += '<tr id="tr-house-info" style="height:100px;"><td colspan="10" class="gendan_box" >';
        infoStr += '<div class="houtab fl">';
        infoStr += '<ul class="padding5">';
        infoStr += '<li style="float:left;width:350px;"><b>' + m.Community;
        switch (m.BusinessKind) {
            case 10: //出售
                var p = (m.SalePrice_T / 10000);

                infoStr += '&nbsp;&nbsp;<span class="red20">售价：' + p.toFixed(2) + '万元</span>';
                break;
            case 20: //出租
                infoStr += '&nbsp;&nbsp;<span class="red20">租价：' + m.RentPrice + '元</span>';
                break;
            case 30: //租售
                var p = (m.SalePrice_T / 10000);

                infoStr += '&nbsp;&nbsp;<span class="red20">售价：' + p.toFixed(2) + '万元</span>';
                infoStr += '&nbsp;&nbsp;<span class="red20">租价：' + m.RentPrice + '元</span>';
                break;
        }

        infoStr += '</b></li>';
        infoStr += '<li style="float:left;width:360px;">地址：' + m.Address + ' ' + ' ' + m.BuildingName + ' ' + m.Room + '</li>';
        infoStr += '<li style="float:left;width:230px;">房源编号：' + m.HouseCode + '</li>';

        if (m.BusinessKind != 20) {
            infoStr += '<li style="float:left;width:110px;">单价：' + (m.SalePrice_T / m.BuildingArea).toFixed(2) + '元</li>';
        }
        infoStr += '<li style="float:left;width:230px;">板块：' + m.Block + '</li>';
        infoStr += '<li style="float:left;width:100px;">类型：' + m.BuildingType + '</li>';
        infoStr += '<li style="float:left;width:230px;">建档日期：' + m.Adddate + '</li>';
        infoStr += '<li style="float:left;width:100px;">装修：' + m.Fitment + '</li>';
        infoStr += '<li style="float:left;width:360px;">';
        infoStr += '<div class="btn_pon_3"><a href="ShowDetailInfo.aspx?houseid=' + m.HouseId + '" target="_blank" title="查看详情" >查看详情</a></div>'
        infoStr += '<input name="btn_viewownerinfo" type="button" class="btn_pon_2" value="查看业主信息"/>';
        infoStr += '<input name="btn_trace" type="button" class="btn_pon" value="我要跟单"/>';
        infoStr += '<input name="btn_houseSend" type="button" class="btn_pon" value="房源推送"/>';
        infoStr += '</li>';
        infoStr += '</ul>';
        infoStr += '</div>';
        infoStr += '<div class="gdtab"><div class="outborder">';
        infoStr += '<div class="infos intitss"><span class="rb"><b>跟单内容</b></span><span class="rbpsn"><b>跟单人</b></span><span class="rbtim"><b>跟单时间</b></span></div>';
        infoStr += '<div id="housetrace_table"></div>';
        infoStr += '</div></div></td</tr>';
        $('#' + this.currentId).after(infoStr);
    },
    createTraceList: function(count) {
        if (!this.tracePage) {
            this.tracePage = new Chenxin.UI.Pagination({
                id: 'housetrace',
                renderTo: 'housetrace_table',
                pageSize: 4,
                actionUrl: 'service/housemgr.ashx?op=gethousetracelist',
                data: { houseid: this.currentId }
            });
        } else {
            this.tracePage.recordCount = 0;
            this.tracePage.hasCount = false;
            this.tracePage.reload({ houseid: this.currentId });
        }
    },
    initEvent: function() {
        var _this = this;
        $('.a_locked_remove').live('click', function() {
            _this.currentId = this.name;
            _this.unLocked();
        });
        $('.a_locked_aleredyremove').live('click', function() {
            alert('该条房源已解封！');
        });

        //点击行数据
        $('.tr_houselist').live('click', function(event) {

            //链接不做点击处理
            if (event.target.tagName == "A") {
                return;
            }
            $('.tr_houselist,.selected_orgbgcolor').each(function() {
                $(this).removeClass('selected_orgbgcolor');
            });

            $(this).toggleClass('selected_orgbgcolor');
            if (!$('#tr-house-info').length) {
                _this.currentId = this.id;
                //_this.houseId = $(this).children("td").eq(10).text();
                _this.loadHouseInfo();

            } else {
                $('#tr-house-info').remove();
                if (_this.currentId != this.id) {
                    _this.currentId = this.id;
                    //_this.houseId = $(this).children("td").eq(10).text();
                    _this.loadHouseInfo();
                }
            }
        });
        $('.tr_houselist').live('mouseover', function() {
            $(this).toggleClass('orgbgcolor');
        });
        $('.tr_houselist').live('mouseout', function() {
            $(this).toggleClass('orgbgcolor');
        });

        //底部按钮点击事件
        $('.LeftFooter a').click(function() {
            _this.bindBottomBar(this);
        });
        $('#keyword').defText({
            deftxt: '请输入地址/编号/楼盘'
        })
        $("input[name='stime']").defText({
            deftxt: '请选择时间'
        })
        $("input[name='etime']").defText({
            deftxt: '请选择时间'
        })
    },
    bindBottomBar: function(obj) {
        switch (obj.name) {
            case 'edit': //房源修改
                if (this.currentHouseInfo) {
                    this.form.permitParam = {
                        model: this.currentHouseInfo.toString()
                    };
                }
                this.actionUrl = 'service/housemgr.ashx';
                this.edit();
                this.actionUrl = 'service/LockedHandler.ashx';
                this.form.setTitle('编辑房源');
                break;
            case 'uploadHousePic': //图片上传
                this.showUploadHousePic();
                break;
            case 'changeconfirmation': //请求变更
                this.showChangeConfirm();
                break;
            case 'record': //备案
                this.showRecordWin();
                break;
            case 'view': //加入带看
                this.addview('remove');
                break;
            case 'key': //钥匙
                this.showKeyWin();
                break;
            case 'commission': //委托
                this.showCommission();
                break;
            case 'report': //报备
                if (!this.currentId) {
					alert('请选择一条房源记录！');
                    return;
                }
                var strUrl = $("#ctl00_foot_hidBusinessRoot").val();
                strUrl = strUrl + "/page/Business_Record/ContractArchive.aspx?HouseId=" + this.currentId;
                window.location.href = strUrl;
                break;
			case 'appPre': //贷款预审
                if (!this.currentId) {
					alert('请选择一条房源记录！');
                    return;
				}
                var strUrl = $("#ctl00_foot_hidBusinessRoot").val();
                strUrl = strUrl + "/page/Preliminary_Info/PreliminaryApply.aspx?HouseId=" + this.currentId;
                window.location.href = strUrl;
                break;
			case 'performance': //业绩申请
                if (!this.currentId) {
					alert('请选择一条房源记录！');
                    return;
				}
                var strUrl = $("#ctl00_foot_hidBusinessRoot").val();
                strUrl = strUrl + "/page/Contract_ApplyInfo/MainBusinessApply.aspx?HouseId=" + this.currentId;
                window.location.href = strUrl;
                break;
            case 'sendtoold': //导出
                this.sendToOld();
                break;
        }
    },
    showUploadHousePic: function() {
        if (!this.currentId) {
            alert('请先选择一条房源记录！');
            return;
        }
        if (!this.showUploadHousePicWin) {
            var _this = this;
            this.showUploadHousePicWin = new Chenxin.UI.UploadHousePicWin({
                scope: this
            });
        }
        this.showUploadHousePicWin.show();
        this.showUploadHousePicWin.setTitle('上传房源图片');
    },
    showChangeConfirm: function() {
        var _this = this;
        if (!this.currentId) {
            alert('请先选择一条房源记录！');
            return;
        }
        if (!this.changeConfirmWin) {
            this.changeConfirmWin = new Chenxin.UI.ChangeConfirmationForm({
                currentId: this.currentId,
                scope: this,
                afterGetFormJson: function(json) {
                    delete json.AddPerson
                    delete json.AddDepart
                    delete json.Address;
                    delete json.nowStatus;
                    if (this.changeConfirmWin.requestUser) {
                        json.RequestUserId = this.changeConfirmWin.requestUser.UserId;
                        json.RequestDepartId = this.changeConfirmWin.requestUser.DepartId;
                        json.RequestUserCode = this.changeConfirmWin.requestUser.UserCode;
                    }
                    if (json.ChangeType == '10') {//状态变更，无需传递此参数
                        if (json.RequestDepartId) {
                            delete json.RequestDepartId;
                        }
                        if (json.RequestUserId) {
                            delete json.RequestUserId;
                        }
                        if (json.RequestDepart) {
                            delete json.RequestDepart;
                        }
                        if (json.RequestUser) {
                            delete json.RequestUser;
                        }
                        if (json.RequestUserCode) {
                            delete json.RequestUserCode;
                        }
                    } else if (json.ChangeType == '20') {//人员变更，无需传递此参数	
                        if (json.HouseStatus) {
                            delete json.HouseStatus;
                        }
                    }
                    json.Mobile = json.request_customerMoblie;
                    json.Phone = json.request_customerPhone;
                    json.CustomerName = json.request_customerName;
                    delete json.request_customerName
                    delete json.request_customerPhone
                    delete json.request_customerMoblie

                    json.HouseId = _this.currentId;
                }
            });
        }
        if (this.changeConfirmWin.show()) {

            this.changeConfirmWin.setTitle('请求变更');
            //var _data = this.currentHouseInfo;
            var _data = {};
            Chenxin.apply(_data, this.currentHouseInfo);
            _data.Remarks = '';
            var temp = this.currentHouseInfo.Status;
            if (temp == 10)
                temp = '有效';
            else if (temp == 20)
                temp = '暂缓';
            else if (temp == -1)
                temp = '无效';
            else if (temp == 30)
                temp = '封盘';
            else
                temp = '';
            _data.nowStatus = temp;
            _data.Address = this.currentHouseInfo.Community + this.currentHouseInfo.BuildingName + this.currentHouseInfo.Room;
            this.changeConfirmWin.fitData(_data);
            $("#request_tr1").show();
            $("#request_tr2").hide();
            $("#request_tr3").hide();
            $("#request_tr4").hide();
            $("#request_tr5").hide();
            $("#request_tr6").hide();
            $("#request_tr7").hide();
            //delete this.currentHouseInfo.nowStatus;
            this.changeConfirmWin.currentId = this.currentId;
        }
    },
    showRecordWin: function() {
        var _this = this;
        if (!this.currentId) {
            alert('请先选择一条房源记录！');
            return;
        }

        if (!this.recordWin) {
            this.recordWin = new Chenxin.UI.RecordForm({
                currentId: this.currentId,
                alwaysCheckPermit: true,
                afterGetFormJson: function(json) {
                    delete json.AddPerson;
                    delete json.AddDepart;
                    json.HouseAddress = json.Address;
                    delete json.Address;
                    delete json.HouseCode;
                    json.HouseId = _this.currentHouseInfo.HouseId;
                }
            });
        }
        if (this.recordWin.show(_this.op, { model: this.currentHouseInfo })) {
            //var _data = this.currentHouseInfo;
            var _data = {};
            Chenxin.apply(_data, this.currentHouseInfo);
            _data.MarkCode = '';
            _data.Address = this.currentHouseInfo.Community + this.currentHouseInfo.BuildingName + this.currentHouseInfo.Room;
            this.recordWin.fitData(_data);
            this.recordWin.setTitle('添加备案');
            this.recordWin.currentId = this.currentId;
        }
    },
    addview: function() {
        var _this = this;
        if (!this.currentId) {
            alert('请先选择一条房源记录！');
            return;
        }
        Chenxin.ajax({
            type: "get",
            url: "service/viewHouse.ashx",
            data: { op: 'addcache', houseid: this.currentId },
            dataType: "json",
            onSuccess: function(data, msg) {
                alert(msg);
            }
        });
    },
    showKeyWin: function() {

        var _this = this;
        if (!this.currentId) {
            alert('请先选择一条房源记录！');
            return;
        }
        if (!this.keyWin) {
            this.keyWin = new Chenxin.UI.KeyForm({
                currentId: this.currentId,
                alwaysCheckPermit: true,
                afterGetFormJson: function(json) {
                    delete json.AddPerson;
                    delete json.AddDepart;
                    json.HouseAddress = json.Address;
                    delete json.Address;
                    json.HouseId = _this.currentHouseInfo.HouseId;
                }
            });
        }
        if (this.keyWin.show(_this.op, { model: this.currentHouseInfo })) {
            //var _data = this.currentHouseInfo;
            var _data = {};
            Chenxin.apply(_data, this.currentHouseInfo);
            _data.Address = this.currentHouseInfo.Community + this.currentHouseInfo.BuildingName + this.currentHouseInfo.Room;
            this.keyWin.fitData(_data);
            this.keyWin.setTitle('钥匙收据登记');
            this.keyWin.currentId = this.currentId;
        }
    },
    showCommission: function() {
        if (!this.currentId) {
            alert('请先选择一条房源记录！');
            return;
        }

        if (!this.commissionWin) {
            var _this = this;
            this.commissionWin = new Chenxin.UI.CommissionForm({
                currentId: this.currentId,
                alwaysCheckPermit: true,
                afterGetFormJson: function(json) {
                    json.HouseId = _this.currentHouseInfo.HouseId;
                    json.Employee = this.employeeId.getText();
                    json.EmpDepart = this.empDepartId.getText();
                    json.EntrustCode = "wt" + json.EntrustCode;
                }
            });
        }

        if (this.commissionWin.show(this.op, { model: this.currentHouseInfo })) {
            this.commissionWin.fitData({
                HouseCode: this.currentHouseInfo.HouseCode,
                HouseAddress: this.currentHouseInfo.Community + this.currentHouseInfo.BuildingName + this.currentHouseInfo.Room
            })
            this.commissionWin.currentId = this.currentId;
        }
    },
    sendToOld: function() {
        if (!this.currentId) {
            alert('请选择一条房源记录！');
            return;
        }
        var _this = this;
        Chenxin.Msg.Waiting.show();
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: 'service/housemgr.ashx',
            data: { op: 'sendtoold', houseid: this.currentId },
            onSuccess: function(data, msg) {
                Chenxin.Msg.Waiting.hide();
                alert(msg);
            },
            onFail: function() {
                Chenxin.Msg.Waiting.hide();
            },
            onWrong: function() {
                Chenxin.Msg.Waiting.hide();
            },
            onError: function() {
                Chenxin.Msg.Waiting.hide();
            }
        });
    },
    unLocked: function() {
        if (!this.currentId) {
            alert('请先选择一条记录！');
            return;
        }
        this.del('unlock', '确定解封吗？');

    },
    initComponent: function() {
        Chenxin.UI.lockedManage.superclass.initComponent.call(this);
    }
});


/**
*查询面板
*/
Chenxin.UI.lockedSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'lock_search',
    initEvent: function() {
        var _this = this;
        $('#btn_search').click(function() {
            _this.scope.reloadGrid();
        });
    },
    createComboList: function() {
        var combl = new Chenxin.UI.StructComboList();
        var structs = combl.comboList;
        
        //房源状态
        var comboLockedStatus = new Chenxin.UI.Combo({
            containerId: '#LockedStatus',
            tbxWidth: 45,
            items: comboCommon.HouseLockStatus
        });
        structs.push(comboLockedStatus);
        return structs;
    },
    initComponent: function() {
        Chenxin.UI.lockedSearch.superclass.initComponent.call(this);
    }
});

$(document).ready(function() {
    var requestMgr = new Chenxin.UI.lockedManage();
});





